package com.smart.primary.util.creat;

import java.io.PrintStream;
import java.lang.reflect.Field;
import java.util.List;

public class CreatController {


	public static void creat(List<Class<?>> cla) {
		try {
			PrintStream ps;
			for (Class<?> cc : cla) {
				ps = new PrintStream(Creat.getPath(Creat.controller)+cc.getSimpleName()+Creat.controller+".java");
				System.setOut(ps);//把创建的打印输出流赋给系统。即系统下次向 ps输出
				getController1(cc,Creat.controller);//输出的内容
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void getController(Class<?> cc,String path) {
		List<Field> field = Creat.getFieldList(cc);
		String str = cc.getName();
		str = StrUtil.subIndOf(str,".",-2);
		str += "."+StrUtil.toLowerOne(Creat.controller)+";";
		System.out.println("package " + str + "\r\n");
		System.out.println(
				"\r\nimport org.apache.commons.lang3.StringUtils;\r\n" +
						"import org.springframework.beans.factory.annotation.Autowired;\r\n" +
						"import org.springframework.data.domain.Page;\r\n" +
						"import org.springframework.data.domain.PageRequest;\r\n" +
						"import org.springframework.data.domain.Pageable;\r\n" +
						"import org.springframework.validation.BindingResult;\r\n" +
						"import org.springframework.web.bind.annotation.RequestMapping;\r\n" +
						"import org.springframework.web.bind.annotation.RestController;\r\n" +
						"\r\n" +
						"import javax.validation.Valid;\r\n" +
						"import java.util.logging.Logger;\r\n");
		System.out.println(
				"@RestController\r\n" +
						"@RequestMapping(" + Creat.onePath + "/" + StrUtil.addStr(cc.getSimpleName(), "/") + "\")\r\n" +
						"public class " + cc.getSimpleName() + "Controller{ \r\n" +
						"    @Autowired\r\n" +
						"    private " + cc.getSimpleName() + "Service " + StrUtil.toLowerOne(cc.getSimpleName()) + "Service;");
/**
 * 获取集合
 */
		System.out.println(
				"    @RequestMapping\r\n" +
						"    public BaseResponse getList(String keyword, Pageable pageRequest){\r\n" +
						"        pageRequest = new PageRequest(pageRequest.getPageNumber()-1>0?pageRequest.getPageNumber()-1:0,pageRequest.getPageSize(),pageRequest.getSort());\r\n" +
						"        Page<" + cc.getSimpleName() + "> page=" + StrUtil.toLowerOne(cc.getSimpleName()) + "Service.getList(keyword,pageRequest);\r\n" +
						"        return successResult(page);\r\n" +
						"    }\r\n");
/**
 * 获取一个
 */
		System.out.println(
				"    @RequestMapping(\"/getOne\")\r\n" +
						"    public BaseResponse getOne(String uuid,String keyword){\r\n" +
						"        if(StringUtils.isBlank(uuid) && StringUtils.isBlank(keyword)){\r\n" +
						"            return new BaseResponse(400,\"请求参数错误!\");\r\n" +
						"        }\r\n" +
						"        return successResult(graitsService.getOne(uuid,keyword));\r\n" +
						"    }\r\n");
/**
 * 添加和修改    	
 */
		System.out.println(
				"    @RequestMapping(\"/save\")\r\n" +
						"    public BaseResponse save(@Valid " + cc.getSimpleName() + " " + StrUtil.toLowerOne(cc.getSimpleName()) + ", BindingResult bindingResult){\r\n" +
						"        if(bindingResult.hasErrors()){\r\n" +
						"            return error(bindingResult);\r\n" +
						"        }else{\r\n" +
						"            " + StrUtil.toLowerOne(cc.getSimpleName()) + "Service.save(" + StrUtil.toLowerOne(cc.getSimpleName()) + ");\r\n" +
						"            return success(\"保存成功！\");\r\n" +
						"        }\r\n" +
						"    }\r\n}");

		/**
		 *导入
		 */
		System.out.println(
				"\t/**\r\n" +
				"\t * 导入\r\n" +
				"\t */\r\n" +
				"\t@RequestMapping(\"upLoad\")\r\n" +
				"\tpublic String upload(@RequestParam(value=\"uploadfile\") MultipartFile file){\r\n" +
				"\t\tString fileName= UploadUtil.upLoad(file,\"/xsl/\",true,true);\r\n" +
				"\t\ttry {\r\n" +
				"\t\t\tWorkbook wb=Workbook.getWorkbook(new File(fileName));\r\n" +
				"\t\t\tSheet ss=wb.getSheet(0);\r\n" +
				"\t\t\tMap<Integer,String> m=new HashMap<Integer,String>();\r\n" +
				"\t\t\tfor(int i=1;i<ss.getRows();i++){\r\n" +
				"\t\t\t\t" + cc.getSimpleName() + " stu=new " + cc.getSimpleName() + "();\r\n" +
				"\t\t\t\tfor (int j=0;j<ss.getColumns();j++) {\r\n" +
				"\t\t\t\t\tm.put(j,ss.getCell(j,i).getContents());\r\n" +
				"\t\t\t\t}");
		int s = 0;
		for (Field fe : field) {
			if ("String".equals(fe.getType().getSimpleName())) {
				System.out.println(
						"\t\t\t\tstu.set" + StrUtil.toUpperOne(fe.getName()) + "(m.get("+s+"));");
			} else {
				if ("Integer".equals(fe.getType().getSimpleName())) {
					System.out.println(
							"\t\t\t\ttry {\r\n" +
							"\t\t\t\t\tstu.set" + StrUtil.toUpperOne(fe.getName()) + "(Integer.parseInt(m.get("+s+")));\r\n" +
							"\t\t\t\t}catch (Exception e){ }");
				} else if ("Double".equals(fe.getType().getSimpleName())) {
					System.out.println(
							"\t\t\t\ttry {\r\n" +
							"\t\t\t\t\tstu.set" + StrUtil.toUpperOne(fe.getName()) + "(Double.parseDouble(m.get("+s+")));\r\n" +
							"\t\t\t\t}catch (Exception e){ }");
				} else if ("BigDecimal".equals(fe.getType().getSimpleName())) {
					System.out.println(
							"\t\t\t\tstu.set" + StrUtil.toUpperOne(fe.getName()) + "(MoneyUtil.getVal2(m.get("+s+")));");
				}  else if ("Date".equals(fe.getType().getSimpleName())) {
					System.out.println(
							"\t\t\t\tstu.set" + StrUtil.toUpperOne(fe.getName()) + "(DateUtil.stringToDate(m.get("+s+")));");
				} else {
					System.out.println(
							"\t\t\t\tstu.set" + StrUtil.toUpperOne(fe.getName()) + "(m.get("+s+"));");
				}
			}
			s++;
		}
		System.out.println(
				"\t\t\t\t" + StrUtil.toLowerOne(cc.getSimpleName()) + "Service.save(stu);\r\n" +
				"\t\t\t\tm.clear();\r\n" +
				"\t\t\t}\r\n" +
				"\t\t} catch (Exception e) {\r\n" +
				"\t\t\te.printStackTrace();\r\n" +
				"\t\t}\r\n" +
				"\t\treturn \"redirect:list\";\r\n" +
				"\t}");
/**
 * 导出
 */
		System.out.println(
				"\t/*\r\n" +
				"\t *导出\r\n" +
				"\t */\r\n" +
				"\t@RequestMapping(\"/down\")\r\n" +
				"\tpublic void down(HttpServletResponse response){\r\n" +
				"\t\tString[] headers = { \"编号\"};\r\n" +
				"\t\tList<" + cc.getSimpleName() + "> list=" + StrUtil.toLowerOne(cc.getSimpleName()) + "Service.getList();\r\n" +
				"\t\tHSSFWorkbook workbook = new HSSFWorkbook();\r\n" +
				"\t\tHSSFSheet sheet = workbook.createSheet();\r\n" +
				"\t\tHSSFRow row = sheet.createRow(0);\r\n" +
				"\t\tfor (short i = 0; i < headers.length; i++) {\r\n" +
				"\t\t\trow.createCell(i).setCellValue(headers[i]);\r\n" +
				"\t\t}\r\n" +
				"\t\tfor(int i=0;i<list.size();i++){\r\n" +
				"\t\t\t" + cc.getSimpleName() + " model=list.get(i);\r\n" +
				"\t\t\trow = sheet.createRow(i+1);\r\n" +
				"\t\t\trow.createCell(0).setCellValue(model.getId());\r\n" +
				"\t\t}\r\n" +
				"\t\ttry {\r\n" +
				"\t\t\tOutputStream out = response.getOutputStream();\r\n" +
				"\t\t\tString fileName = \"code.xls\";// 文件名\r\n" +
				"\t\t\tresponse.setContentType(\"application/x-msdownload\");\r\n" +
				"\t\t\tresponse.setHeader(\"Content-Disposition\", \"attachment; filename=\"+ URLEncoder.encode(fileName, \"UTF-8\"));\r\n" +
				"\t\t\tworkbook.write(out);\r\n" +
				"\t\t\tout.close();\r\n" +
				"\t\t} catch (Exception e) {\r\n" +
				"\t\t\te.printStackTrace();\r\n" +
				"\t\t}\r\n" +
				"\t}");
	}

	public static void getController1(Class<?> cc,String path) {
		System.out.println(
				"package "+Creat.pack+"."+Creat.controller+";\r\n" );

		System.out.println(
				"import com.smart.buss.service." + cc.getSimpleName() + "Service;\r\n" +
				"import com.smart.buss.vo." + cc.getSimpleName() + ";\r\n" +
				"import com.smart.primary.common.common.BaseCtrl;\r\n" +
				"import com.smart.primary.common.util.AjaxResult;\r\n" +
				"import com.smart.primary.common.util.Util;\r\n" +
				"import org.apache.commons.lang3.StringUtils;\r\n" +
				"import org.springframework.beans.factory.annotation.Autowired;\r\n" +
				"import org.springframework.web.bind.annotation.RequestMapping;\r\n" +
				"import org.springframework.web.bind.annotation.RestController;\r\n" +
				"import java.util.List;\r\n");

		System.out.println(
				"@RestController\r\n" +
				"@RequestMapping(\"" + Creat.onePath + "/" + StrUtil.addStr(cc.getSimpleName(), "/") + "\")\r\n" +
				"public class " + cc.getSimpleName() + "Controller extends BaseCtrl {\r\n\r\n" +
				"\t@Autowired\r\n" +
				"\tprivate " + cc.getSimpleName() + "Service " + StrUtil.toLowerOne(cc.getSimpleName()) + "Service;\r\n");

		System.out.println(
				"\t@RequestMapping\r\n" +
				"\tpublic AjaxResult getList(" + cc.getSimpleName() + " entity) throws Exception{\r\n" +
				"\t\tList<" + cc.getSimpleName() + "> list=" + StrUtil.toLowerOne(cc.getSimpleName()) + "Service.queryListByPage(entity);\r\n" +
				"\t\treturn getResult(entity, list);\r\n" +
				"\t}\r\n");

		System.out.println(
				"\t@RequestMapping(\"list\")\r\n" +
				"\tpublic List<" + cc.getSimpleName() + "> list(" + cc.getSimpleName() + " entity) throws Exception{\r\n" +
				"\t\treturn " + StrUtil.toLowerOne(cc.getSimpleName()) + "Service.queryList(entity);\r\n" +
				"\t}\r\n");
		System.out.println(
				"\t@RequestMapping(\"/save\")\r\n" +
				"\tpublic AjaxResult save(" + cc.getSimpleName() + " entity) throws Exception{\r\n" +
				"\t\tif(StringUtils.isBlank(entity.getId())){\r\n" +
				"\t\t\tentity.setId(Util.getUUID());\r\n" +
				"\t\t\t" + StrUtil.toLowerOne(cc.getSimpleName()) + "Service.insert(entity);\r\n" +
				"\t\t\treturn AjaxResult.updateSuccess();\r\n" +
				"\t\t}else {\r\n" +
				"\t\t\t" + StrUtil.toLowerOne(cc.getSimpleName()) + "Service.update(entity);\r\n" +
				"\t\t\treturn AjaxResult.saveSuccess();\r\n" +
				"\t\t}\r\n" +
				"\t}\r\n");

		System.out.println(
				"\t@RequestMapping(\"/del\")\r\n" +
				"\tpublic AjaxResult del(String id) throws Exception{\r\n" +
				"\t\t" + StrUtil.toLowerOne(cc.getSimpleName()) + "Service.delete(id);\r\n" +
				"\t\treturn AjaxResult.deleteSuccess();\r\n" +
				"\t}\r\n" +
				"}");
	}
}
